
<template>
  <el-form ref="form" :model="data" :rules="rules" label-width="100px">
                    <el-form-item label="标题" prop="title">
                        <el-input v-model.trim="data.title" placeholder="请填写标题" />
                    </el-form-item>
                     <el-form-item label="简介" prop="sub_title">
                        <el-input type="textarea" v-model.trim="data.sub_title" placeholder="请填写简介"  rows="3" />
                    </el-form-item>
                      <el-form-item label="活动时间">
                      <el-col :span="11">
                        <el-date-picker type="date" placeholder="开始日期"  value-format="yyyy-MM-dd" v-model="data.start_time" style="width: 100%;"></el-date-picker>
                      </el-col>
                      <el-col class="line" :span="2" style="text-align:center">-</el-col>
                      <el-col :span="11">
                        <el-date-picker type="date" placeholder="结束日期" v-model="data.end_time" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
                      </el-col>
                    </el-form-item>

                    <el-form-item label="商品图片" prop="goods_thumb">
                        <el-upload
                        :action="$store.state.settings.file_upload_url"
                        :headers ="headerObj"
                        :limit=1
                        list-type="picture-card"
                        :file-list="data.goods_thumbfileList"
                        accept="image/png, image/jpeg, image/gif, image/jpg"
                        :on-success="handlegoods_thumbSuccess"
                        :before-upload="beforegoods_thumbUpload"
                        :on-exceed="handlegoods_thumbExceed"
                        :on-preview="handlegoods_thumbPreview"
                        :on-remove="handlegoods_thumbRemove">
                        <i class="el-icon-plus"></i>
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="商品原价" prop="goods_price">
                        <el-input v-model.trim="data.goods_price" placeholder="请填写商品原价" />
                    </el-form-item>
                    <el-form-item label="秒杀价" prop="price">
                        <el-input v-model.trim="data.price" placeholder="请填写商品秒杀价" />
                    </el-form-item>
                    <el-form-item label="库存" prop="stock">
                        <el-input-number v-model="data.stock" :min="1" :max="10" label="库存"></el-input-number>
                    </el-form-item>

                    <el-form-item label="详情" prop="detail">
                         <tinymce
                    id="editorId"
                  ref="editor"
                  v-model="data.detail"
                    @input="handleInput"
                    @onClick="handleClick"
                  />
                    </el-form-item>
                    <el-form-item label="上架">
                             <el-switch v-model="data.is_show"></el-switch>
                    </el-form-item>

    <el-dialog :visible.sync="dialogVisible" width="30%" append-to-body>
              <img width="100%" :src="previewUrl" alt="">
    </el-dialog>  
    <el-form-item>
      <el-button type="primary" :disabled="$store.state.settings.ajaxButton" @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { create } from '@/api/seckill'
import { getToken } from '@/utils/auth'
import tinymce from '@/components/Tinymce'


export default {
  name: 'seckill_create',
  components: {tinymce },
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      loading:false,
      dialogVisible:false,
      previewUrl:'',
      headerObj: {
            'Authorization': 'Bearer ' + getToken()
        },
      rules: {
                    title: [ 
                { required: true, message: "请输入标题", trigger: "blur" },
                  { min: 3,max:10, message: "长度3到10个字", trigger: "blur" }
            ], 
            start_time: [ 
                { required: true, message: "请输入开始时间", trigger: "blur" }
            ], 
            end_time: [ 
                { required: true, message: "请输入结束时间", trigger: "blur" }
            ], 
           
            goods_price: [ 
                { required: true, message: "请输入商品原价", trigger: "blur" }
            ], 
            stock: [ 
                { required: true, message: "请输入库存", trigger: "blur" }
            ], 
            price: [ 
                { required: true, message: "请输入秒杀价格", trigger: "blur" }
            ], 
  
      },
      data: {
                    title:'',
            sub_title:'',
            start_time:'',
            end_time:'',
            goods_thumb:'',
            goods_thumbfileList:[],
            goods_price:'',
            stock:'',
            price:'',
            min_price:'',
            max_price:'',
            self_bargain:1,
            detail:'',
            is_show:true,

      }
    }
  },
  created() {
  },
  methods: {
          handlegoods_thumbExceed(){ 
                this.$message.error('只能上传一张图片');
             },
            handlegoods_thumbPreview(file){
                 this.dialogVisible = true;
                 this.previewUrl = file.url;
             },
            handlegoods_thumbRemove(){
                 this.data.goods_thumb = '';
             },
             handlegoods_thumbSuccess(response, file, fileList) {
               this.$message.success('上传成功');
                this.$store.commit('settings/SET_AJAX_BUTTON', false);
                this.data.goods_thumb= response.data.src;
            },
             beforegoods_thumbUpload(file) {
                this.$store.commit('settings/SET_AJAX_BUTTON', true);
                const isJPG = file.type === "image/jpeg";
                const isPNG = file.type === "image/png";
                const isLt5M = file.size / 1024 / 1024 < 5;
                if (!isJPG && !isPNG) {
                this.$message.error("上传图片只能是 JPG 、PNG格式!");
                }
                if (!isLt5M) {
                this.$message.error("上传图片大小不能超过 2MB!");
                }
                
            },
          handleInput(value) {
            },
          handleClick(e, editor) {
          },
          clear() {
                this.$refs.editor.clear()
          },

    
    submit() {
 
      const thiz = this

      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }
        create({
                       title:this.data.title,
            sub_title:this.data.sub_title,
            start_time:this.data.start_time,
            end_time:this.data.end_time,
            goods_thumb:this.data.goods_thumb,
            goods_price:this.data.goods_price,
            stock:this.data.stock,
            price:this.data.price,
            min_price:this.data.min_price,
            max_price:this.data.max_price,
            self_bargain:this.data.self_bargain,
            detail:this.data.detail,
            is_show:this.data.is_show ?1:0,

        }).then(response => {
          this.$store.commit('settings/SET_AJAX_BUTTON', true);
          this.$message({
            message: '添加成功',
            type: 'success',
            duration: 1500,
            onClose() {
              if (thiz.$refs.form !== undefined) {
                thiz.$refs.form.resetFields()
              }
              thiz.item.dialogVisible = false
            }
          })
        })
      })
    }
  }
}
</script>
